<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Kefu extends Common
{
    // 客服列表
    public function index() {
        $kefu = db('kefu')->select();
        
        $this->assign('kefu', $kefu);
        return view();
    }

    // 添加/编辑客服
    public function save_kefu_page() {
        $id = input('param.id');
        if (Request::instance()->isPost()) {
            // 提交数据
            $data = [
                'type' => input('param.type'),
                'accounts' => input('param.accounts'),
                'add_time' => time()
            ];

            if ($id > 0)  {
                $text = '编辑';
                $rs = db('kefu')->where('id', $id)->update($data);
            } else {
                $text = '添加';
                $rs = db('kefu')->insert($data);
            }
            if ($rs)
                return json(['code' => 200, 'msg' => $text . '客服成功！', 'url' => url('index')]);
            else
                return json(['code' => 0, 'msg' => $text . '客服失败！']);
        } else {
            $text = $id > 0 ? '编辑' : '添加';
            $kefu = [];
            if ($id > 0) {
                $kefu = db('kefu')->where('id', $id)->find();
            }
            $this->assign('kefu', $kefu);
            $this->assign('text', $text);
            return view();
        }
    }
}
